<?php 
	class UserContentModel
	{

		function __construct()
		{

		}
		function get_user_image($user_id,$limit)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT * FROM ".$wpdb->prefix.'bwg_image WHERE author=%d ORDER BY RAND() LIMIT %d',$user_id,$limit);
			$row=$wpdb->get_results($query);
			return $row;
		}
		function get_user_id_by_name($user_name)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT ID FROM ".$wpdb->prefix.'users WHERE user_login=%s',$user_name);
			$row=$wpdb->get_row($query);
			return $row->ID;
		}
		function get_user_info($user_id)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT * FROM ".$wpdb->prefix.'users WHERE ID=%d',$user_id);
			$row=$wpdb->get_row($query);
			return $row;
		}
		function get_style($url)
		{
			if($url=='') return;
			$upload_dir = wp_upload_dir();
			$imagesize=getimagesize($upload_dir['basedir'].'/photo-gallery'.$url);
			$width=$imagesize[0];
			$height=$imagesize[1];
			if($width>$height)
				$style="max-width:none;height:100%;width:auto;";
			else
				$style="max-height:none;width:100%;height:auto;";
			return $style;
		}
		function get_image_count($user_id)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT * FROM ".$wpdb->prefix.'bwg_image WHERE author=%d ',$user_id);
			$row=$wpdb->get_results($query);
			return count($row);
		}
		function get_album_count($user_id)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT * FROM ".$wpdb->prefix.'bwg_gallery WHERE author=%d',$user_id);
			$row=$wpdb->get_results($query);
			return count($row);
		}
		function get_like_count($user_id)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT * FROM ".$wpdb->prefix.'ulike WHERE user_id=%d AND status="like" ',$user_id);
			$row=$wpdb->get_results($query);
			return count($row);
		}
		function user_follow_status($follower_id,$user_id)
		{
			global $wpdb;

			$query=$wpdb->prepare("SELECT status FROM ".$wpdb->prefix.'users_follow WHERE follower_id=%d AND author_id=%d ',$follower_id,$user_id);
			$row=$wpdb->get_row($query);
			return $row->status;
		}
		function user_has_follow($follower_id,$user_id)
		{

			global $wpdb;
			$query=$wpdb->prepare("SELECT status FROM ".$wpdb->prefix.'users_follow WHERE follower_id=%d AND author_id=%d ',$follower_id,$user_id);
			$row=$wpdb->get_results($query);
			if($row) return 1;
			else return 0;
		}
		function update_follow_status($follower_id,$user_id)
		{
			global $wpdb;
			if($this->user_has_follow($follower_id,$user_id))
			{
				$status=$this->user_follow_status($follower_id,$user_id) ;
				if ($status==1) $status=0; 
				else  $status=1; 
				$wpdb->update( 
					$wpdb->prefix.'users_follow', 
					array( 
						'status' => $status,
					), 
					array( 'follower_id' => $follower_id ,'author_id' => $user_id ), 
					array('%d'),
					array( '%d' ,'%d')
				);
			}
			else
			{
				$wpdb->insert( 
					$wpdb->prefix.'users_follow',
					array( 'follower_id' => $follower_id ,'author_id' => $user_id,'status'=>1), 
					array( '%d' ,'%d','%d')
				);
			}
			
		}
		function get_follower_count($author_id)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT * FROM ".$wpdb->prefix.'users_follow WHERE author_id=%d AND status=1',$author_id);
			$row=$wpdb->get_results($query);
			return count($row);
		}
		function get_following_count($follower_id)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT * FROM ".$wpdb->prefix.'users_follow WHERE follower_id=%d AND status=1 ',$follower_id);
			$row=$wpdb->get_results($query);
			return count($row);
		}
		function get_followers($author_id)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT * FROM ".$wpdb->prefix.'users_follow WHERE author_id=%d AND status=1',$author_id);
			$row=$wpdb->get_results($query);
			return $row;
		}
		function get_followings($author_id)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT * FROM ".$wpdb->prefix.'users_follow WHERE follower_id=%d AND status=1',$author_id);
			$row=$wpdb->get_results($query);
			return $row;
		}
		
	}
?>